import { useRef } from 'react'
import { message, Modal } from 'antd'

import InfoForm from './InfoForm'
import { useAppSelector } from '@/utils/hooks'

import type { RecordItem } from '../type'
import { roleTypeMap } from '@/utils/consts'
import { userUpdate } from "@/api/modules/auth";

interface Props {
  open?: boolean
  selRecord?: RecordItem
  onCancel: () => void
  onSearch?: (params: any) => void
}

const InfoEditModal: React.FC<Props> = ({ open, onCancel, selRecord, onSearch }) => {
  const [app] = useAppSelector(state => [state.app])
  const infoFromRef = useRef<any>(null)

  const handleCancel = () => {
    onCancel()
  }

  const handleOk = async () => {
    try {
      const values = await infoFromRef.current.validateFields()
			userUpdate({
          ...values,
          id: selRecord?.id,
					tenantId: app.currentEntity.entityId
      }).then(res => {
        if (res.code === 0) {
          message.success('修改成功')
          onSearch?.({})
          onCancel()
        }
      })
    } catch (error) {
      console.log(error)
    }
  }

  return (
    <Modal
      title="用户信息修改"
      width={640}
      maskClosable={false}
      open={open}
      bodyStyle={{ maxHeight: 550, overflowY: 'auto' }}
      onCancel={handleCancel}
      onOk={handleOk}
    >
      <InfoForm ref={infoFromRef} selRecord={selRecord}></InfoForm>
    </Modal>
  )
}

export default InfoEditModal
